//
//  AppDelegate.m
//  DogWalk
//
//  Created by Mac on 15/11/15.
//  Copyright © 2015年 riverlj. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "CoreDataStack.h"
@interface AppDelegate ()
{
    CoreDataStack *coreDataStack;
}
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UINavigationController *navCtl = (UINavigationController *)self.window.rootViewController;
    ViewController *viewController = (ViewController *)navCtl.topViewController;
    coreDataStack = [[CoreDataStack alloc]initWithStoreURL:self.storeURL modelURL:self.modelURL];
    viewController.managedContext = [coreDataStack managedObjectContext];
    return YES;
}

- (NSURL *)storeURL {
    /**
     *  NSFileManager.defaultManager().URLsForDirectory(
     .DocumentDirectory, inDomains: .UserDomainMask)
     */
    NSURL *documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
    return [documentsDirectory URLByAppendingPathComponent:@"DogWalk.sqlite"];
}

- (NSURL *)modelURL {
    /**
     *  NSBundle.mainBundle()
     .URLForResource(self.modelName,
     withExtension: "momd")!
     */
    return [[NSBundle mainBundle]URLForResource:@"DogWalk" withExtension:@"momd"];
}

-(void)applicationDidEnterBackground:(UIApplication *)application{
    [[coreDataStack managedObjectContext] save:nil];
}


@end
